package com.swlz.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.swlz.commons.DBUtil;
import com.swlz.entity.FictionBuy;

import java.util.Date;
/**
 * <pre>
 * 数据访问对象
 * 小说消费清单
 * TableName : FICTION_BUY
 * ClassName : com.swlz.dao.FictionBuyDAO
 * Author :swlz
 * E-Mail : 2233875735@qq.com
 * Date : 2020-07-01 09:13:52
 * Version : 1.0
 * Copyright 2020 swlz.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. It is forbidden to divulge it or use it for other commercial purposes
 * </pre>
 **/
public class FictionBuyDAO {

	/**
	 * 添加一个小说消费清单
	 * 包含所有字段
	 * @param fictionBuy - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int insert(FictionBuy fictionBuy) throws SQLException {
		if(fictionBuy.getBuyId()==null) {
			fictionBuy.setBuyId(DBUtil.nextId());
		}
		return DBUtil.insert(fictionBuy);
	}
 
	
	/**
	 * 添加一个小说消费清单
	 * 只包含非null的字段
	 * @param fictionBuy - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int insertSelective(FictionBuy fictionBuy) throws SQLException {
		if(fictionBuy.getBuyId()==null) {
			fictionBuy.setBuyId(DBUtil.nextId());
		}
		return DBUtil.insertSelective(fictionBuy);
	}
	

	/**
	 * 根据主键更新一个小说消费清单
	 * 包含所有字段
	 * @param fictionBuy - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int update(FictionBuy fictionBuy) throws SQLException {
		return DBUtil.update(fictionBuy);
	}
	
	/**
	 * 根据主键更新一个小说消费清单
	 * 只包含非null的字段
	 * @param fictionBuy - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int updateSelective(FictionBuy fictionBuy) throws SQLException {
		return DBUtil.updateSelective(fictionBuy);
	}
	
	/**
	 * 根据主键删除一个小说消费清单
	 * @param buyId - 主键
	 * @return
	 * @throws SQLException
	 */
	public int deleteById(Long buyId) throws SQLException  {
		return DBUtil.execute("DELETE FROM FICTION_BUY WHERE BUY_ID=?", buyId);
	}
	
	/**
	 * 根据主键获取一个小说消费清单
	 * @param buyId - 主键
	 * @return
	 * @throws SQLException 没有获取到，或者获取多个都会抛出异常
	 */
	public FictionBuy selectById(Long buyId) throws SQLException  {
		return DBUtil.get("SELECT * FROM FICTION_BUY WHERE BUY_ID=?", FictionBuy.class ,buyId);
	}

		
	/**
	 * 根据模型中的非空字段查询
	 * @param buyId - 实体对象
	 * @return
	 * @throws SQLException 没有获取到，或者获取多个都会抛出异常
	 */
	public FictionBuy selectByModel(FictionBuy fictionBuy)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT * FROM FICTION_BUY WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(fictionBuy,sql,params);
		return DBUtil.get(sql.toString(), FictionBuy.class, params.toArray());
	}
	
	/**
	 * 根据模型中的非空字段查询
	 * @param buyId - 实体对象
	 * @param index - 开始索引
	 * @param len - 返回长度
	 * @return
	 * @throws SQLException
	 */
	public List<FictionBuy> selectByModel(FictionBuy fictionBuy,int index,int len)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT * FROM FICTION_BUY WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(fictionBuy,sql,params);
		sql.append(" ORDER BY BUY_ID DESC LIMIT ?,?");
		params.add(index);
		params.add(len);
		return DBUtil.list(sql.toString(), FictionBuy.class, params.toArray());
	}

	/**
	 * 根据模型中的非空字段统计
	 * @param buyId - 实体对象
	 * @return
	 * @throws SQLException
	 */
	public long countByModel(FictionBuy fictionBuy)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT count(*) as c FROM FICTION_BUY WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(fictionBuy,sql,params);
		return DBUtil.getLong(sql.toString(), params.toArray());
	}
	
	/**
	 * 格式化where条件和参数
	 * @param buyId - 实体对象
	 * @param sql - SQL语句
	 * @param params - SQL语句中的参数
	 */
	private void where(FictionBuy fictionBuy,StringBuffer sql,List<Object> params) {
		if(fictionBuy==null){
			return;
		}

		if(fictionBuy.getBuyId()!=null) {
			sql.append(" AND BUY_ID=?");
			params.add(fictionBuy.getBuyId());
		}

		if(fictionBuy.getSectionId()!=null) {
			sql.append(" AND SECTION_ID=?");
			params.add(fictionBuy.getSectionId());
		}

		if(fictionBuy.getUserId()!=null) {
			sql.append(" AND USER_ID=?");
			params.add(fictionBuy.getUserId());
		}

		if(fictionBuy.getBuyScore()!=null) {
			sql.append(" AND BUY_SCORE=?");
			params.add(fictionBuy.getBuyScore());
		}

		if(fictionBuy.getBuyTime()!=null) {
			sql.append(" AND BUY_TIME=?");
			params.add(fictionBuy.getBuyTime());
		}

	}
}